import React from 'react';
import { Route, Redirect } from 'react-router-dom';

const AuthRouter = ({ component: Component, ...rest }) => {
  // 检查用户是否已登录
  const isAuthenticated = () => {
    const userInfo = localStorage.getItem('userInfo');
    return userInfo && JSON.parse(userInfo);
  };

  return (
    <Route
      {...rest}
      render={props =>
        isAuthenticated() ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{
              pathname: "/login",
              state: { from: props.location }
            }}
          />
        )
      }
    />
  );
};

export default AuthRouter; 